package com.zyfdroid.backupftpserver.ftplets;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import org.apache.ftpserver.ftplet.FtpFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

public class AppPackageFtpFile implements FtpFile {

    public AppPackageFtpFile(PackageManager pm,String filename,String packageName) throws PackageManager.NameNotFoundException {
        this.packageName = packageName;
        PackageInfo pi= pm.getPackageInfo(packageName,0);
        packagePath = pi.applicationInfo.sourceDir;
        packageFile = new File(packagePath);
        displayName=filename;
    }

    private String displayName;
    private String packageName;
    private String packagePath;
    private File packageFile;

    @Override
    public String getAbsolutePath() {
        return "/app/"+getName();
    }

    @Override
    public String getName() {
        return displayName;
    }

    @Override
    public boolean isHidden() {
        return false;
    }

    @Override
    public boolean isDirectory() {
        return false;
    }

    @Override
    public boolean isFile() {
        return true;
    }

    @Override
    public boolean doesExist() {
        return packageFile.exists();
    }

    @Override
    public boolean isReadable() {
        return true;
    }

    @Override
    public boolean isWritable() {
        return false;
    }

    @Override
    public boolean isRemovable() {
        return false;
    }

    @Override
    public String getOwnerName() {
        return "PM";
    }

    @Override
    public String getGroupName() {
        return "SYSTEM";
    }

    @Override
    public int getLinkCount() {
        return 1;
    }

    @Override
    public long getLastModified() {
        return packageFile.lastModified();
    }

    @Override
    public boolean setLastModified(long l) {
        return false;
    }

    @Override
    public long getSize() {
        return packageFile.length();
    }

    @Override
    public Object getPhysicalFile() {
        return packageFile;
    }

    @Override
    public boolean mkdir() {
        return false;
    }

    @Override
    public boolean delete() {
        return false;
    }

    @Override
    public boolean move(FtpFile ftpFile) {
        return false;
    }

    @Override
    public List<? extends FtpFile> listFiles() {
        return null;
    }

    @Override
    public OutputStream createOutputStream(long l) throws IOException {
        throw new IOException("Not writable");
    }

    @Override
    public InputStream createInputStream(final long l) throws IOException {
        return new FileInputStream(packageFile){{skip(l);}};
    }
}
